<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
>
<head th:include="includes/head :: htmlhead(~{::title}, ~{})">
    <meta charset="utf-8">
    <title>数据变更申请</title>
</head>
<body>
<div id="mainLayout" class="easyui-layout" data-options="fit: true">
    <div style="padding:0px;height:calc(100% - 30px)">
        <table id='dg' style="width:100%;height:100%"></table>
    </div>
    <div id="pg" style="height:30px" class="easyui-pagination">
    </div>
</div>

<!-- 添加模块 -->
<div id="adddlg" class="easyui-dialog" style="width:600px;height:420px;padding:10px 20px"
     closed="true" closable="false" modal="true" buttons="#dlg-buttons">
    <form id="addfm" method="post" novalidate>
        <input name="id" type="hidden"/>
        <table cellpadding="5">
            <tr>
                <td>数据库选择:</td>
                <td>
                    <select id="databaseSelect" class="easyui-combobox" style="width:420px;" data-options="panelHeight:'auto'"
                            required="true" missingMessage="请选择数据库！"></select>
                </td>
            </tr>
            <tr>
                <td>要执行的sql:</td>
                <td><input name="applyContent" class="easyui-textbox" data-options="multiline:true" style="height:100px;width:420px" type="text"
                           required="true" missingMessage="不能为空"/></td>
            </tr>
            <tr>
                <td>回退操作语句:</td>
                <td><input name="backContent" class="easyui-textbox" data-options="multiline:true" style="height:100px;width:420px" type="text"
                           required="true" missingMessage="不能为空"/></td>
            </tr>
            <tr>
                <td>影响的行数:</td>
                <td><input name="affectLineNum" class="easyui-textbox" style="width:420px" type="number" required="true" missingMessage="不能为空"/></td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#adddlg').dialog('close')">取消</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveAddFm()">保存</a>
</div>

<!-- check模块 -->
<div id="checkDlg" class="easyui-dialog" style="width:600px;height:420px;padding:10px 20px"
     closed="true" closable="false" modal="true" buttons="#checkDlg-buttons">
    <div id="checkTable">
    </div>
</div>
<div id="checkDlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#checkDlg').dialog('close')">取消</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-diy-accept1" onclick="check(1)">审核通过</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-diy-reject1" onclick="check(2)">拒绝</a>
</div>

<script type="text/javascript">
    let ctxPath = '[[${#servletContext.contextPath}]]';
    let userType = [[${userType}]];

    $(function () {
        $(window).resize(function () {
            alert(2)
        });

        let toolbar = [{
                text: '申请',
                iconCls: 'icon-diy-add1',
                handler: function () {
                    openAddDlg();
                }
            }, {
                text: '删除',
                iconCls: 'icon-diy-delete1',
                handler: function () {
                    del();
                }
            }, {
                text: '修改',
                iconCls: 'icon-diy-edit1',
                handler: function () {
                    openEditDlg();
                }
            }
            ]
        ;
        if (userType == 1) {
            toolbar.push({
                text: '审核',
                iconCls: 'icon-diy-check1',
                handler: function () {
                    openCheckDlg();
                }
            });
        }
        toolbar.push('-');
        toolbar.push({
            text: '刷新',
            iconCls: 'icon-diy-refresh1',
            handler: function () {
                pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
            }
        });

        $('#dg').datagrid({
            data: []
            , columns: [
                [
                    {field: 'id', title: 'id', resizable: true, hidden: true}
                    , {field: 'applyUserId', title: '申请用户编号', resizable: true, hidden: true}
                    , {field: 'applyUserName', title: '申请用户姓名', resizable: true}
                    , {field: 'applyDate', title: '申请日期', resizable: true}
                    , {field: 'applyTime', title: '申请时间', resizable: true}
                    , {field: 'databaseId', title: '数据库编号', resizable: true, hidden: true}
                    , {field: 'databaseName', title: '数据库名称', resizable: true}
                    , {field: 'applyStatus', title: '申请状态', resizable: true, formatter: formatApplyStatus}
                    , {field: 'runStatus', title: '执行状态', resizable: true, formatter: formatRunStatus}
                    , {field: 'affectLineNum', title: '影响行数', resizable: true}
                    , {field: 'applyContent', title: '请求的内容', resizable: true}
                    , {field: 'backContent', title: '出问题时回退', resizable: true}
                    , {field: 'checkUserId', title: '审核用户编号', resizable: true, hidden: true}
                    , {field: 'checkUserName', title: '审核用户姓名', resizable: true}
                    , {field: 'checkDate', title: '审核日期', resizable: true}
                    , {field: 'checkTime', title: '审核时间', resizable: true}
                ]
            ]
            , autoRowHeight: true
            , rownumbers: true
            , singleSelect: true
            , toolbar: toolbar
        });

        $('#pg').pagination({
            pageList: COMMONPARAM_PAGELIST,
            pageSize: COMMONPARAM_DEFAULTPAGESIZE,
            total: 0,
            pageNumber: 1,
            beforePageText: '第',
            afterPageText: '页    共 {pages} 页',
            displayMsg: '当前显示 {from} - {to}条记录,共{total}条记录'
        });

        pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
    });

    function pagingList(pageNumber, pageSize) {
        let submitData = {};
        submitData['page'] = pageNumber;
        submitData['limit'] = pageSize;
        postJSON(ctxPath + '/mdmsDatachangeApply/pagingList', submitData,
            function (data) {
                $('#dg').datagrid({
                    data: data['list']
                })
                $('#pg').pagination({
                    pageList: COMMONPARAM_PAGELIST,
                    pageSize: data['limit'],
                    total: data['total'],
                    pageNumber: data['page'],
                    beforePageText: '第',
                    afterPageText: '页    共 {pages} 页',
                    displayMsg: '当前显示 {from} - {to}条记录,共{total}条记录',
                    onSelectPage: function (pageNumber, pageSize) {
                        pagingList(pageNumber, pageSize);//选择第几页的时候，就刷新
                    },
                });
            }, function (result, resultdesc) {
                $.messager.show({
                    title: '异常',
                    msg: result + '-' + resultdesc,
                    showType: 'show'
                });
            }, function () {
                alert(3)
            }, function () {
                $.messager.progress(COMMONPARAM_PROCESS);
            }, function () {
                $.messager.progress('close');
            });
    }

    function formatApplyStatus(val, row) {
        if (val == '0') {
            return '审核中';
        } else if (val == '1') {
            return '<span style="color:green;">审核通过</span>';
        } else if (val == '2') {
            return '<span style="color:red;">审核拒绝</span>';
        } else {
            return '<span style="color:red;">未知</span>';
        }
    }

    function formatRunStatus(val, row) {
        if (val == '0') {
            return '未执行';
        } else if (val == '1') {
            return '<span style="color:green;">执行成功</span>';
        } else if (val == '2') {
            return '<span style="color:red;">执行失败</span>';
        } else {
            return '<span style="color:red;">未知</span>';
        }
    }


    function openAddDlg() {
        COMMONPARAM_WEBOPERATION = 'add';
        $('#adddlg').dialog('open').dialog('setTitle', '添加数据库变更申请');
        $('#addfm').form('clear');
    }

    function openEditDlg() {
        COMMONPARAM_WEBOPERATION = 'edit';
        let row = $('#dg').datagrid('getSelected');
        if (row) {
            if (row['applyStatus'] != 0) {
                $.messager.show({
                    title: '提示',
                    msg: '只有初始状态的记录才能修改',
                    showType: 'show'
                });
                return;
            }

            $('#adddlg').dialog('open').dialog('setTitle', '修改数据库变更申请');
            $('#addfm').form('load', row);
            $(databaseSelect).combobox("setValue", row.databaseId);
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一条记录!',
                showType: 'show'
            });
        }
    }

    function saveAddFm() {
        if (!$('#addfm').form('validate')) {
            return;
        }

        let submitData = geneSubmitDataFromForm("#addfm", {});
        submitData["databaseId"] = $(databaseSelect).combobox("getValue");
        submitData["databaseName"] = $(databaseSelect).combobox("getText");

        if (COMMONPARAM_WEBOPERATION == 'add') {
            delete submitData['id'];
        }
        postJSON(ctxPath + '/mdmsDatachangeApply/' + COMMONPARAM_WEBOPERATION, submitData,
            function (data) {
                pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
                $('#adddlg').dialog('close');
            }, function (result, resultdesc) {
                $.messager.show({
                    title: '异常',
                    msg: result + '-' + resultdesc,
                    showType: 'show'
                });
            }, function () {
                alert(3)
            }, function () {
                $.messager.progress(COMMONPARAM_PROCESS);
            }, function () {
                $.messager.progress('close');
            });
    }

    function del() {
        COMMONPARAM_WEBOPERATION = 'del';
        let row = $('#dg').datagrid('getSelected');
        if (row) {
            if (row['applyStatus'] != 0) {
                $.messager.show({
                    title: '提示',
                    msg: '只有初始状态的记录才能删除',
                    showType: 'show'
                });
                return;
            }

            $.messager.confirm('提示', '你确定要删除该条记录?', function (r) {
                if (r) {
                    let submitData = {};
                    submitData['id'] = row['id'];
                    postJSON(ctxPath + '/mdmsDatachangeApply/' + COMMONPARAM_WEBOPERATION, submitData,
                        function (data) {
                            pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
                        }, function (result, resultdesc) {
                            $.messager.show({
                                title: '异常',
                                msg: result + '-' + resultdesc,
                                showType: 'show'
                            });
                        }, function () {
                            alert(3)
                        }, function () {
                            $.messager.progress(COMMONPARAM_PROCESS);
                        }, function () {
                            $.messager.progress('close');
                        });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一条记录!',
                showType: 'show'
            });
        }
    }

    function openCheckDlg() {
        let row = $('#dg').datagrid('getSelected');
        if (row) {
            if (row['applyStatus'] != 0) {
                $.messager.show({
                    title: '提示',
                    msg: '只有初始状态的记录才能审核',
                    showType: 'show'
                });
                return;
            }

            let table = "<table cellpadding='5'>"
            table += "<tr>";
            table += "<td>申请人姓名:</td>";
            table += "<td>" + row['applyUserName'] + "</td>";
            table += "</tr>";
            table += "<tr>";
            table += "<td>申请日期:</td>";
            table += "<td>" + row['applyDate'] + "</td>";
            table += "</tr>";
            table += "<td>申请时间:</td>";
            table += "<td>" + row['applyTime'] + "</td>";
            table += "</tr>";
            table += "<td>数据库:</td>";
            table += "<td>" + row['databaseName'] + "</td>";
            table += "</tr>";
            table += "<td>请求执行内容:</td>";
            let applyContent = row['applyContent'];
            let reg = new RegExp('\r\n', 'g');
            table += "<td>" + applyContent.replace(reg, '<br/>') + "</td>";
            table += "</tr>";
            table += "<td>回退脚本:</td>";
            table += "<td>" + row['backContent'] + "</td>";
            table += "</tr>";
            table += "<td>影响行数:</td>";
            table += "<td>" + row['affectLineNum'] + "</td>";
            table += "</tr>";
            table += "</table>";
            //columnModeDlg
            $('#checkDlg').dialog('open').dialog('setTitle', '审核');
            $('#checkTable').html(table);

        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一条记录!',
                showType: 'show'
            });
        }
    }

    function check(applyStatus) {
        let row = $('#dg').datagrid('getSelected');
        if (row) {
            let applyStatusDesc = '';
            if (applyStatus == 1) {
                applyStatusDesc = '审核通过';
            } else {
                applyStatusDesc = '拒绝';
            }
            $.messager.confirm('提示', '你确定要' + applyStatusDesc + '该条记录?', function (r) {
                if (r) {
                    let submitData = {};
                    submitData['id'] = row['id'];
                    submitData['applyStatus'] = applyStatus;
                    postJSON(ctxPath + '/mdmsDatachangeApply/check', submitData,
                        function (data) {
                            javascript:$('#checkDlg').dialog('close');
                            $.messager.show({
                                title: '提示',
                                msg: '审核完成',
                                showType: 'show'
                            });
                            pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
                        }, function (result, resultdesc) {
                            javascript:$('#checkDlg').dialog('close');
                            $.messager.show({
                                title: '异常',
                                msg: result + '-' + resultdesc,
                                showType: 'show'
                            });
                            pagingList(1, COMMONPARAM_DEFAULTPAGESIZE);
                        }, function () {
                            alert(3)
                        }, function () {
                            $.messager.progress(COMMONPARAM_PROCESS);
                        }, function () {
                            $.messager.progress('close');
                        });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '请选择一条记录!',
                showType: 'show'
            });
        }
    }

    //列出当前用户拥有的所有数据库
    let submitData = {};
    postJSON(ctxPath + '/mdmsDatabase/queryMyDatabases', submitData,
        function (data) {
            $('#databaseSelect').combobox({
                data: data['list']
                , valueField: 'id'
                , textField: 'databaseName'
            });
        }, function (result, resultdesc) {
            $.messager.show({
                title: '异常',
                msg: result + '-' + resultdesc,
                showType: 'show'
            });
        }, function () {
            alert(3)
        }, function () {
            $.messager.progress(COMMONPARAM_PROCESS);
        }, function () {
            $.messager.progress('close');
        });


</script>
</body>